package cn.nexd.app.collector.widget.sliding;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.nexd.app.collector.R;

/**
 * Created by xun on 2016/8/29.
 */
public class SlidingView extends RelativeLayout implements OnSlidingListener {
    private RelativeLayout slidingView;
    private SlidingBlock slidingBlock;
    private TextView slidingLabel;
    private Context context;
    private OnSlidingListener onSlidingListener;
    private String slidingText;
    private String slidingField;
    private boolean isStart = false;
    private boolean isReset = false;
    private static final String TAG = "SlidingView";

    public void setSlidingText(String slidingText) {
        this.slidingText = slidingText;
        slidingLabel.setText(slidingText);

    }

    public void reset() {
        if (slidingBlock != null) {
            slidingBlock.reset();
            isStart = false;
            isReset = true;
        }
    }

    public void setOnSlidingListener(OnSlidingListener onSlidingListener) {
        this.onSlidingListener = onSlidingListener;
    }


    public SlidingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public void setSlidingField(String slidingField) {
        this.slidingField = slidingField;
        setSlidingText("滑动开启" + slidingField + "采集");
    }

    private void initView(Context context, AttributeSet attrs) {
        this.context = context;
        slidingView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.view_sliding_block, (ViewGroup) getParent(), false);
        addView(slidingView);
        slidingBlock = (SlidingBlock) slidingView.findViewById(R.id.btn_sliding_block);
        slidingLabel = (TextView) slidingView.findViewById(R.id.sliding_collector_label);
        slidingBlock.setSlidingListener(this);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingViewStyle);
        slidingField = a.getString(R.styleable.SlidingViewStyle_sliding_Label);
        if (!TextUtils.isEmpty(slidingField)) {
            setSlidingText("滑动开启" + slidingField + "采集");
        } else {
            setSlidingText("滑动开启采集");
            setEnabled(false);
        }
        if (a != null) {
            a.recycle();
        }

    }


    @Override
    public void onLeft() {
        if (isStart) {
            slidingBlock.setBackground(ContextCompat.getDrawable(context, R.drawable.sliding_block_bg));
            slidingBlock.setImageResource(R.drawable.icon_play);
            slidingLabel.setBackground(ContextCompat.getDrawable(context, R.drawable.btn_sliding_bg));
            if (!(TextUtils.isEmpty(slidingField))) {
                slidingLabel.setText("滑动开启" + slidingField + "采集");
                if (onSlidingListener != null) {
                    onSlidingListener.onLeft();
                }
                isStart = false;
            }
        }

    }

    @Override
    public void onRight() {
        if (!isStart) {
            if (!(TextUtils.isEmpty(slidingField))) {
                if (onSlidingListener != null) {
                    onSlidingListener.onRight();
                }
                if (!isReset) {
                    slidingBlock.setBackground(ContextCompat.getDrawable(context, R.drawable.sliding_block_stop_bg));
                    slidingBlock.setImageResource(R.drawable.icon_stop);
                    slidingLabel.setBackground(ContextCompat.getDrawable(context, R.drawable.btn_sliding_stop_bg));
                    slidingLabel.setText("滑动结束" + slidingField + "采集");
                    isStart = true;
                } else {
                    isReset = false;
                }
            }
        }
    }


}
